#include<algorithm>
#include<vector>
using std::sort;
using std::vector;


class Solution {
public:
    vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
        sort(people.begin(), people.end(), [](vector<int>& a, vector<int>& b) {
            return a[0] < b[0] || (a[0] == b[0] && a[1] > b[1]);
        });
        vector<vector<int>> q(people.size(), {-1, -1});
        for (int i = 0; i < people.size(); i++) {
            int k = people[i][1];
            int count = 0;
            int j = 0;
            for (; j < q.size(); j++) {
                if (q[j][1] == -1) count++;
                if (count == k+1) break;
            }
            q[j][0] = people[i][0];
            q[j][1] = people[i][1];

        }
        return q;
    }
};